Ext.define('YXMES.sqcm.controller.GroupFormulaController', {
	extend : 'Ext.app.Controller',
	refs : [ {
		selector : 'sqcm_groupformula',
		ref : 'sqcmgroupformula'
	} ],
	requires : [ 'YXMES.sqcm.store.BatteryGroupFormulaStore', 'YXMES.sqcm.store.ProductStore' ],
	stores : [],
	models : [],
	views : [ 'YXMES.sqcm.view.GroupFormulaView' ],
	subcontrollers : [  ],
	_init : false,
	init : function() {
		if (this._init)
			return;
		this._init = true;
		this.control({
			'sqcm_groupformula' : {
				afterrender : function() {
					return this.handle(this.onAfterrender, arguments);
				},
				destroy : function() {
					return this.handle(this.onDestroy, arguments);
				}
			},
			'sqcm_groupformula #btnAdd' : {
				click : function() {
					return this.handle(this.onBtnAddClick, arguments);
				}
			},
			'sqcm_groupformula #btnRemove' : {
				click : function() {
					return this.handle(this.onBtnRemoveClick, arguments);
				}
			},
			'sqcm_groupformula #btnSave' : {
				click : function() {
					return this.handle(this.onBtnSaveClick, arguments);
				}
			},
			'sqcm_groupformula #btnSearch' : {
				click : function() {
					return this.handle(this.onBtnSearchClick, arguments);
				}
			}
		});
		if (!Ext.isEmpty(this.subcontrollers)) {
			Ext.each(this.subcontrollers, function(name) {
				this.getController(name).init();
			}, this);
		}
		// User definied init
	},
	mainView : undefined,
	getMainView : function() {
		return this.mainView;
	},
	getScopeView : function() {
		return this.getMainView().scopeView || {};
	},
	getInput : function() {
		return this.getScopeView().input || {};
	},
	setInput : function(data) {
		this.getMainView().input = data;
	},
	getOutput : function() {
		return this.getMainView().output || {};
	},
	setOutput : function(data) {
		this.getScopeView().output = data;
	},
	handle : function(func, args) {
		var item = args && args.length > 0 && Ext.typeOf(args[0]) === 'object' ? args[0] : undefined;
		if (item) {
			try {
				this.mainView = (item.xtype == 'sqcm_groupformula' ? item : item.up('sqcm_groupformula'));
			} catch(e) {
			}
		}
		try {
			return func.apply(this, args);
		} finally {
		}
	},
	getItem : function(itemId) {
		return itemId===this.getMainView().getItemId()?this.getMainView():this.getMainView().down('#' + itemId);
	},
	getStore : function(name) {
		if (name == 'BatteryGroupFormulaStore')
			return this.getMainView().SqcmViewBatteryGroupFormulaServiceStore;
		if (name == 'ProductStore')
			return this.getMainView().SbasViewProductServiceStore;
		return undefined;
	},
	bind : function(view) {
		view = (view ? view : this.getMainView());
		view.SqcmViewBatteryGroupFormulaServiceStore = Ext.create('YXMES.sqcm.store.BatteryGroupFormulaStore', {
			pageSize : 30
		});
		view.SbasViewProductServiceStore = Ext.create('YXMES.sqcm.store.ProductStore', {
			pageSize : 30,
			autoDestory : true,
			autoLoad : true
		});
		view.down('#grdGroup').reconfigure(view.SqcmViewBatteryGroupFormulaServiceStore);
		view.down('#cdvMatCode').bindStore(view.SbasViewProductServiceStore);
		view.SqcmViewBatteryGroupFormulaServiceStore.on('load', function(store, records, successful, eOpts) {
			this.getItem('frmSearch').getForm().setValues(store.getProxy().getReader().rawData);
		}, this);
	},
	onAfterrender : function(view, eOpts) {
		this.bind();
		// User defined afterrender
	},
	onDestroy : function(view, eOpts) {
		// User defined destory
		var view = this.getMainView();
		if (view.SqcmViewBatteryGroupFormulaServiceStore) {
			view.SqcmViewBatteryGroupFormulaServiceStore.destroyStore();
			view.SqcmViewBatteryGroupFormulaServiceStore = undefined;
		}
		if (view.SbasViewProductServiceStore) {
			view.SbasViewProductServiceStore.destroyStore();
			view.SbasViewProductServiceStore = undefined;
		}
		if (this.getMainView().input)
			this.getMainView().input = undefined;
		if (this.getMainView().output)
			this.getMainView().output = undefined;
	},
	onBtnAddClick : function(button, e, eOpts) {
		this.getItem('grdGroup').getStore().add({});
	},
	onBtnRemoveClick : function(button, e, eOpts) {
		var rcds = this.getItem('grdGroup').getSelectionModel().getSelection();
		if (Ext.isEmpty(rcds)) {
		    return;
		}
		
		this.getItem('grdGroup').getStore().remove(rcds);
	},
	onBtnSaveClick : function(button, e, eOpts) {
		if (!this.getItem('frmSearch').isValid()) {
		    return;
		}
		
		var frmVals = this.getItem('frmSearch').getValues();
		var gridData = [];
		this.getItem('grdGroup').getStore().each(function(rcd) {
		    if (!Ext.isEmpty(rcd.get('levelNo'))) {
		        gridData.push(rcd.data);
		    }
		});
		
		var me = this;
		Ext.Msg.show({
		    title: T.label.save,
		    msg: T.msg.are_you_sure_to_save_data,
		    icon: Ext.Msg.QUESTION,
		    buttons: Ext.Msg.YESNO,
		    fn: function(btnId) {
		        if ('yes' == btnId) {
		            button.setDisabled(true);
		
		            YX.ZbusRpc.request({
		                module: 'SqcmSaveBatteryGroupFormulaService',
		                showSuccess: true,
		                params: {
		                    levelDef: frmVals,
		                    levelDat: gridData
		                },
		                success: function(rsp) {
		                },
		                callback: function() {
		                    button.setDisabled(false);
		                }
		            });
		        }
		    }
		});
	},
	onBtnSearchClick : function(button, e, eOpts) {
		if (!this.getItem('cdvMatCode').isValid()) {
		    return;
		}
		
		this.loadBatteryGroupFormulaStore({
		    matCode: this.getItem('cdvMatCode').getValue(),
		    levelVer: this.getItem('txtLevelVer').getValue()
		});
	},
	loadBatteryGroupFormulaStore : function(params, options) {
		params = Ext.applyIf(params||{}, {
			// Default params
		});
		
		var store = this.getStore('BatteryGroupFormulaStore');
		if(!store) {
			return false;
		}
		
		store.getProxy().extraParams = params;
		if(store.loadPage){
			store.loadPage(1, options);
		}else{
			store.load(options);
		}
	},
	loadProductStore : function(params, options) {
		params = Ext.applyIf(params||{}, {
			// Default params
		});
		
		var store = this.getStore('ProductStore');
		if(!store) {
			return false;
		}
		
		store.getProxy().extraParams = params;
		if(store.loadPage){
			store.loadPage(1, options);
		}else{
			store.load(options);
		}
	}
});